/**
 * projectName: 47movie-project
 * fileName: MyExcetionHandler.java
 * packageName: com.woniu.gateway.handler
 * date: 2021-01-08 10:48 上午
 * copyright(c) 2017-2020 xxx公司
 */
package com.damie.gateway.handler;

import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @version: V1.0
 * @author: light
 * @className: MyExcetionHandler
 * @packageName: com.woniu.gateway.handler
 * @description:
 * @data: 2021-01-08 10:48 上午
 **/
@Component
public class MyExcetionHandler implements ErrorWebExceptionHandler {
    @Override
    public Mono<Void> handle(ServerWebExchange serverWebExchange, Throwable throwable) {
        serverWebExchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
        return serverWebExchange.getResponse().writeWith(Mono.fromSupplier(()->{
            if (throwable.getClass()==NoTokenException.class){
                return serverWebExchange.getResponse().bufferFactory().wrap(("没有token"+throwable.getMessage()).getBytes());
            }else if (throwable.getClass()==TokenIsNotException.class){
                return serverWebExchange.getResponse().bufferFactory().wrap(("token不正确"+throwable.getMessage()).getBytes());
            }
            return serverWebExchange.getResponse().bufferFactory().wrap(("服务器异常"+throwable.getMessage()).getBytes());
        }));
    }
}
